package linyetao.rbac.service;

import linyetao.rbac.domain.dto.UserDto;
import linyetao.rbac.domain.entity.Dept;
import linyetao.rbac.domain.entity.Role;
import linyetao.rbac.domain.entity.User;
import linyetao.rbac.domain.vo.Result;
import linyetao.rbac.domain.vo.UserVo;
import java.util.Arrays;


public class UserService {
    public static int DATA_SIZE = 100;
    public static User[] data = new User[DATA_SIZE+1];
    public static int idCounter = 1;

    public static UserVo convertToVo(User user){
        String deptName = "";
        Dept dept = DeptService.findById(user.deptId);
        if (dept != null) {
            deptName = dept.name;
        }
        String roleName = "";
        Role role = RoleService.findById(user.roleId);
        if (role != null) {
            roleName = role.name;
        }
        return new UserVo(user.id, user.username, user.name, user.mobile, user.email, user.deptId, user.roleId, user.enabled, deptName, roleName);
    }

    public static User createEntity(UserDto dto, int id){
        User user = new User(dto.username, dto.name, dto.mobile, dto.email, dto.deptId, dto.roleId);
        user.id = id;
        return user;
    }

    public static void updateEntity(UserDto dto, User user) {
        user.username = dto.username;
        user.name = dto.name;
        user.mobile = dto.mobile;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }

    public static Result list(){
        UserVo[] temp = new UserVo[DATA_SIZE];
        int length = 0;
        for (int i = 1; i < idCounter; i++) {
            if (data[i] != null) {
                temp[length++] = convertToVo(data[i]);
            }
        }
        return Result.success("查询成功", Arrays.copyOf(temp, length));
    }

    public static Result create(UserDto dto) {
        if (idCounter > DATA_SIZE) {
            return Result.failure("创建失败，超出存储容量");
        }
        if (!DeptService.existsById(dto.deptId)) {
            return Result.failure("创建失败，不存在该部⻔");
        }
        if (!RoleService.existsById(dto.roleId)) {
            return Result.failure("创建失败，不存在该⻆⾊");
        }
        User user = createEntity(dto, idCounter++);
        data[user.id] = user;
        return Result.success("创建成功", user);
    }

    public static boolean existsById(int id) {
        return id >= 1 && id < idCounter && data[id] != null;
    }

    public static Result update(UserDto dto, int id) {
        if (id < 1 || id >= idCounter || data[id] == null) {
            return Result.failure("更新失败，不存在该数据");
        }
        if (!UserService.existsById(dto.deptId)) {
            return Result.failure("更新失败，不存在该部⻔");
        }
        if (!RoleService.existsById(dto.roleId)) {
            return Result.failure("更新失败，不存在该⻆⾊");
        }
        User user = data[id];
        updateEntity(dto, user);
        return Result.success("更新成功", null);
    }

    public static boolean deptInUse(int deptId) {
        boolean inUse = false;
        for (User user : data) {
            if (user != null && user.deptId == deptId) {
                inUse = true;
                break;
            }
        }
        return inUse;
    }
    public static boolean roleInUse(int roleId) {
        boolean inUse = false;
        for (User user : data) {
            if (user != null && user.roleId == roleId) {
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    public static Result toggle(int id) {
        if (id < 1 || id >= idCounter || data[id] == null) {
            return Result.failure("切换状态失败，不存在该数据");
        }
        data[id].enabled = !data[id].enabled;
        return Result.success(data[id].enabled ? "启⽤成功" : "禁⽤成功", null);
    }

}
